package com.neusoft.zqygbc.neusoft_wenda.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.JudgeStatus;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.Status;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.UserInfo;
import com.neusoft.zqygbc.neusoft_wenda.model.FollowModel;
import com.neusoft.zqygbc.neusoft_wenda.model.UserModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;

public class UserInfoActivity extends BaseAvtivity {
    private Context context;
    private ImageView userinfo_return;
    private ImageView userinfo_photo;
    private TextView userinfo_btn_follow;
    private TextView userinfo_username;
    private LinearLayout userinfo_create;
    private LinearLayout userinfo_collect;
    private LinearLayout userinfo_follow;
    private Intent intent;
    private FollowModel followModel;
    private Call call;
    private RetrofitCallback<JudgeStatus> judgeStatusCallback;
    private RetrofitCallback<Status> followCallback, delFollowCallback, statusCallback;
    private String followstatus;
    private UserModel userModel;
    private RetrofitCallback<UserInfo> userInfoRetrofitCallback;

    @Override
    void initViews() {
        setLayout(R.layout.activity_user_info);
        intent = getIntent();
        userinfo_return = (ImageView) findViewById(R.id.userinfo_return);
        userinfo_btn_follow = (TextView) findViewById(R.id.userinfo_btn_follow);
        userinfo_username = (TextView) findViewById(R.id.userinfo_username);
        userinfo_photo = (ImageView) findViewById(R.id.userinfo_photo);
        userinfo_create = (LinearLayout) findViewById(R.id.userinfo_create);
        userinfo_collect = (LinearLayout) findViewById(R.id.userinfo_collect);
        userinfo_follow = (LinearLayout) findViewById(R.id.userinfo_follow);
        isFollow();
    }

    @Override
    void initEvents() {
        userinfo_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent_collect = new Intent(UserInfoActivity.this, HisCreaterActivity.class);
                intent_collect.putExtra("user_id", intent.getStringExtra("question_uid"));
                startActivity(intent_collect);
            }
        });
        userinfo_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        userinfo_btn_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (followstatus.equals("0")) {
                    follow();
                } else {
                    delFollow();
                }
            }
        });


        userinfo_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_collect = new Intent(UserInfoActivity.this, MyCollectActivity.class);
                intent_collect.putExtra("user_id", intent.getStringExtra("question_uid"));
                startActivity(intent_collect);

            }
        });
        userinfo_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_collect = new Intent(UserInfoActivity.this, MyFollowActivity.class);
                intent_collect.putExtra("user_id", intent.getStringExtra("question_uid"));
                startActivity(intent_collect);

            }
        });

    }

    @Override
    void initData() {
        userModel = new UserModel();
        call = userModel.getuserinfo(intent.getStringExtra("question_uid"));
        userInfoRetrofitCallback = new RetrofitCallback<UserInfo>() {
            @Override
            public void onResponse(final UserInfo userInfo) {
                userinfo_username.setText(userInfo.getUsername());
                Picasso.with(context).load("http://115.159.93.244/wenda/Public/userphoto/" + userInfo.getUpic()).into(userinfo_photo);
            }
        };
        userModel.bindCallback(call, userInfoRetrofitCallback);

    }

    private void isFollow() {
        followModel = new FollowModel();
        call = followModel.isfollow(getUser_id(), intent.getStringExtra("question_uid"));
        judgeStatusCallback = new RetrofitCallback<JudgeStatus>() {
            @Override
            public void onResponse(JudgeStatus judgeStatus) {
                followstatus = judgeStatus.getCode();
                if (followstatus.equals("0")) {
                    userinfo_btn_follow.setText("+未关注");
                    userinfo_btn_follow.setTextColor(getResources().getColor(R.color.buttontextcolor));
                } else {
                    userinfo_btn_follow.setText("√已关注");
                    userinfo_btn_follow.setTextColor(getResources().getColor(R.color.textcolor));
                }
            }
        };
        followModel.bindCallback(call, judgeStatusCallback);
    }


    private void follow() {
        followModel = new FollowModel();
        call = followModel.follow(getUser_id(), intent.getStringExtra("question_uid"));
        followCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(UserInfoActivity.this, "已关注", Toast.LENGTH_SHORT).show();
                    userinfo_btn_follow.setText("√已关注");
                    userinfo_btn_follow.setTextColor(getResources().getColor(R.color.textcolor));
                    followstatus = "1";
                } else {
                    Toast.makeText(UserInfoActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        followModel.bindCallback(call, followCallback);
    }

    private void delFollow() {
        followModel = new FollowModel();
        call = followModel.delfollow(getUser_id(), intent.getStringExtra("question_uid"));
        delFollowCallback = new RetrofitCallback<Status>() {
            @Override
            public void onResponse(Status status) {
                if (status.getCode().equals("1")) {
                    Toast.makeText(UserInfoActivity.this, "关注取消", Toast.LENGTH_SHORT).show();
                    userinfo_btn_follow.setText("+未关注");
                    userinfo_btn_follow.setTextColor(getResources().getColor(R.color.buttontextcolor));
                    followstatus = "0";
                } else {
                    Toast.makeText(UserInfoActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                }
            }
        };
        followModel.bindCallback(call, delFollowCallback);
    }
}
